<?php
/**
 * 报量单控制器
 */

namespace app\school\controller;

use app\common\model\Dose;
use app\common\model\Meal;
use app\common\model\Menu;
use app\common\validate\DoseValidate;
use app\school\exception\ApiServiceException;
use app\school\service\DoseService;
use hg\apidoc\annotation as Apidoc;
use think\facade\Config;
use think\response\Json;

/**
 * @Apidoc\Title("报量单详情")
 * @Apidoc\Group("index")
 */
class DoseController extends ApiBaseController {
	/**
	 * @Apidoc\Title("报量单列表")
	 * @Apidoc\Desc("列表")
	 * @Apidoc\Method("GET")
	 * @Apidoc\Url("/school/dose/index")
	 * @Apidoc\Returned("data",        type="int",                 desc="desc")
	 */
	public function index(DoseService $service): Json {
		try {
			$this->param['school_id'] = $this->uid;
			$data = $service->getList($this->param, $this->page, $this->limit);
			$result = [
				'dose' => $data,
			];

			return api_success($result);
		} catch (ApiServiceException $e) {
			return api_error($e->getMessage());
		}
	}

	/**
	 * 添加
	 * @param DoseValidate $validate
	 * @param DoseService $service
	 * @return Json
	 */
	public function add(DoseValidate $validate, DoseService $service): Json {
		$check = $validate->scene('api_add')->check($this->param);
		if (!$check) {
			return api_error($validate->getError());
		}

		$result = $service->createData($this->param);

		return $result ? api_success() : api_error();
	}

	/**
	 * @Apidoc\Title("详情")
	 * @Apidoc\Desc("列表")
	 * @Apidoc\Method("GET")
	 * @Apidoc\Url("/school/dose/info")
	 * @Apidoc\Param("id",          type="string",require=true, desc="id")
	 * @Apidoc\Returned("data",        type="int",                 desc="desc")
	 */
	public function info(DoseValidate $validate, DoseService $service): Json {
		$check = $validate->scene('api_info')->check($this->param);
		if (!$check) {
			return api_error($validate->getError());
		}

		try {

			$result = $service->getDataInfo($this->id);
			return api_success([
				'dose' => $result,
			]);

		} catch (ApiServiceException $e) {
			return api_error($e->getMessage());
		}
	}

	/**
	 * @Apidoc\Title("编辑")
	 * @Apidoc\Desc("编辑")
	 * @Apidoc\Method("POSt")
	 * @Apidoc\Url("/school/dose/edit")
	 * @Apidoc\Param("id",          type="string",require=true, desc="id")
	 * @Apidoc\Returned("data",        type="int",                 desc="desc")
	 */
	public function edit(DoseService $service, DoseValidate $validate): Json {
		$week = date('w');
		if ($week == 0) {
			$week = 7;
		}
		$config = Config::get('site.site');
		if ($week > $config['week']) {
			return api_error('已截止提交下周的报量单');
		}
		$content = $this->param['content'];
		foreach ($content as $key => $value) {
			$detail = $value['detail'];
			if ($value['tag'] == 0) {
				// code...
			} else {
				foreach ($value['detail'] as $dk => $dv) {
					$menu_id = [];
					foreach ($dv['meal'] as $mk => $mv) {
						$menu_detail = [];
						foreach ($mv['menu'] as $mmk => $mmv) {
							$menu_detail[] = $mmv;
						}
						$menu = Menu::where('id', $mv['menu_id'])->findOrEmpty();
						if ($menu->isEmpty() || $menu['detail'] != $menu_detail) {
							$menu_data = [
								'name' => $mv['menu_name'],
								'old_id' => $mv['menu_id'],
								'tag' => 1,
								'cate'=>1,
								'detail' => $menu_detail,
							];
							$menu = Menu::create($menu_data);
							$menu_id[]['menu_id'] = $menu->id;
						} else {
							$menu_id[]['menu_id'] = $mv['menu_id'];
						}

					}
					$meal = Meal::where('id', $dv['meal_id'])->findOrEmpty();
					if ($meal->isEmpty() || $meal['detail'] != $meal_id) {
						$meal_data = [
							'name' => $dv['meal_name'],
							'old_id' => $dv['meal_id'],
							'tag' => 1,
							'cate'=>1,
							'detail' => $menu_id,
						];
						$meal = Meal::create($meal_data);
						$detail[$dk]['meal_id'] = $meal->id;
					} else {
						$detail[$dk]['meal_id'] = $dv['meal_id'];
					}
				}
			}

			$update_data = [
				'detail' => $detail,
				'detail2' => $value['detail2'],
			];
			Dose::where('id', $value['id'])->update($update_data);
		}
		return api_success();

	}

	/**
	 * 删除
	 * @param DoseService $service
	 * @param DoseValidate $validate
	 * @return Json
	 */
	public function del(DoseService $service, DoseValidate $validate): Json {
		$check = $validate->scene('api_del')->check($this->param);
		if (!$check) {
			return api_error($validate->getError());
		}

		try {
			$service->deleteData($this->id);
			return api_success();
		} catch (ApiServiceException $e) {
			return api_error($e->getMessage());
		}
	}

}
